home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / 2d / poly.c < prev    next >
C/C++ Source or Header  |  1998-03-03  |  7KB  |  312 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: /usr/CVS/descent/2d/poly.c,v $
  15.  * $Revision: 1.1.1.1 $
  16.  * $Author: nobody $
  17.  * $Date: 1998/03/03 15:11:47 $
  18.  *
  19.  * Graphical routines for drawing polygons.
  20.  *
  21.  * $Log: poly.c,v $
  22.  * Revision 1.1.1.1  1998/03/03 15:11:47  nobody
  23.  * reimport after crash from backup
  24.  *
  25.  * Revision 1.1.1.1  1998/02/13  20:21:22  hfrieden
  26.  * Initial Import
  27.  *
  28.  * Revision 1.1  1995/03/09  09:21:47  allender
  29.  * Initial revision
  30.  *
  31.  *
  32.  * --- PC RCS information --
  33.  * Revision 1.5  1994/11/13  13:03:43  john
  34.  * Added paged out bit in bitmap structure.  Commented out the
  35.  * poly code that is never used.
  36.  * 
  37.  * Revision 1.4  1994/03/14  16:56:13  john
  38.  * Changed grs_bitmap structure to include bm_flags.
  39.  * 
  40.  * Revision 1.3  1993/10/15  16:23:14  john
  41.  * y
  42.  * 
  43.  * Revision 1.2  1993/10/08  14:30:39  john
  44.  * *** empty log message ***
  45.  * 
  46.  * Revision 1.1  1993/09/08  11:44:13  john
  47.  * Initial revision
  48.  * 
  49.  *
  50.  */
  51.  
  52. #include "mem.h"
  53. #include "gr.h"
  54. #include "grdef.h"
  55.  
  56. //#define USE_POLY_CODE 1
  57.  
  58. #define  MAX_SCAN_LINES 1200
  59.  
  60. #ifdef USE_POLY_CODE 
  61.  
  62. int y_edge_list[MAX_SCAN_LINES];
  63.  
  64. void gr_upoly(int nverts, int *vert )
  65. {           
  66.     int temp;
  67.     int startx, stopx;  // X coordinates of both ends of current edge.
  68.     int firstx, firsty; // Saved copy of the first vertex to connect later.
  69.     int dx_dy;          // Slope of current edge.
  70.     int miny, maxy;
  71.  
  72.     int starty, stopy;  // Y coordinates of both ends of current edge.
  73.  
  74.     int x1, x2, i;
  75.  
  76.     // Find the min and max rows to clear out the minimun y_edge_list.
  77.     // (Is it worth it?)
  78.     maxy = vert[1];
  79.     miny = vert[1];
  80.  
  81.     for (i=3; i<(nverts*2); i+=2 )
  82.     {
  83.         if (vert[i]>maxy) maxy=vert[i];
  84.         if (vert[i]<miny) miny=vert[i];
  85.     }
  86.  
  87.     miny >>= 16;
  88.     miny--;             // -1 to be safe
  89.     maxy >>= 16;
  90.     maxy++;             // +1 to be safe
  91.  
  92.     // Clear only the part of the y_edge_list that w will be using
  93.     if (miny < 0) miny = 0;
  94.     if (maxy > MAX_SCAN_LINES) maxy = MAX_SCAN_LINES;
  95.  
  96.     for (i=miny; i<maxy; i++ )
  97.         y_edge_list[i] = -1;
  98.  
  99.     // Save the first vertex so that we can connect to it at the end.
  100.     firstx = vert[0];
  101.     firsty = vert[1] >> 16;
  102.  
  103.     do
  104.     {
  105.         nverts--;
  106.  
  107.         // Get the beginning coordinates of the current edge.
  108.         startx = vert[0];
  109.         starty = vert[1] >> 16;
  110.  
  111.         // Get the ending coordinates of the current edge.
  112.         if (nverts > 0 ) {
  113.             stopx = vert[2];
  114.             stopy = vert[3] >> 16;
  115.             vert += 2;
  116.         } else  {
  117.             stopx = firstx;     // Last edge, uses first vertex as endpoint
  118.             stopy = firsty;
  119.         }
  120.  
  121.         if (stopy < starty )    {
  122.             temp = stopy;
  123.             stopy = starty;
  124.             starty = temp;
  125.             temp = stopx;
  126.             stopx = startx;
  127.             startx = temp;
  128.         }
  129.  
  130.         if (stopy == starty )
  131.         {
  132.             // Draw a edge going horizontally across screen
  133.             x1 = startx>>16;
  134.             x2 = stopx>>16;
  135.  
  136.             if (x2 > x1 )
  137.                 //gr_uscanline( x1, x2-1, stopy );
  138.                 gr_uscanline( x1, x2, stopy );
  139.             else
  140.                 //gr_uscanline( x2, x1-1, stopy );
  141.                 gr_uscanline( x2, x1, stopy );
  142.  
  143.         } else  {
  144.  
  145.             dx_dy = (stopx - startx) / (stopy - starty);
  146.  
  147.             for (; starty < stopy; starty++ )
  148.             {
  149.                 if (y_edge_list[starty]==-1)
  150.                     y_edge_list[starty] = startx;
  151.                 else    {
  152.                     x1 = y_edge_list[starty]>>16;
  153.                     x2 = startx>>16;
  154.  
  155.                     if (x2 > x1 )
  156.                         //gr_uscanline( x1, x2-1, starty );
  157.                         gr_uscanline( x1, x2, starty );
  158.                     else
  159.                         //gr_uscanline( x2, x1-1, starty );
  160.                         gr_uscanline( x2, x1, starty );
  161.                 }
  162.                 startx += dx_dy;
  163.             }
  164.         }
  165.  
  166.  
  167.     } while (nverts > 0);
  168. }
  169.  
  170.  
  171. void gr_poly(int nverts, int *vert )
  172. {
  173.     int temp;
  174.     int startx, stopx;  // X coordinates of both ends of current edge.
  175.     int firstx, firsty; // Saved copy of the first vertex to connect later.
  176.     int dx_dy;          // Slope of current edge.
  177.     int miny, maxy;
  178.  
  179.     int starty, stopy;  // Y coordinates of both ends of current edge.
  180.  
  181.     int x1, x2, i, j;
  182.  
  183.     // Find the min and max rows to clear out the minimun y_edge_list.
  184.     // (Is it worth it?)
  185.     maxy = vert[1];
  186.     miny = vert[1];
  187.  
  188.     j = 0;
  189.  
  190.     for (i=3; i<(nverts*2); i+=2 )
  191.     {
  192.         if (vert[i]>maxy) {
  193.             if ((maxy=vert[i]) > MAXY) j++;
  194.             //if (j>1) break;
  195.         }
  196.  
  197.         if (vert[i]<miny) {
  198.             if ((miny=vert[i]) < MINY) j++;
  199.             //if (j>1) break;
  200.         }
  201.     }
  202.  
  203.     miny >>= 16;
  204.     miny--;         // -1 to be safe
  205.     maxy >>= 16;
  206.     maxy++;          // +1 to be safe
  207.  
  208.     if (miny < MINY) miny = MINY;
  209.     if (maxy > MAXY) maxy = MAXY+1;
  210.  
  211.     // Clear only the part of the y_edge_list that w will be using
  212.     for (i=miny; i<maxy; i++ )
  213.        y_edge_list[i] = -1;
  214.  
  215.     // Save the first vertex so that we can connect to it at the end.
  216.     firstx = vert[0];
  217.     firsty = vert[1] >> 16;
  218.  
  219.     do
  220.     {
  221.         nverts--;
  222.  
  223.         // Get the beginning coordinates of the current edge.
  224.         startx = vert[0];
  225.         starty = vert[1] >> 16;
  226.  
  227.         // Get the ending coordinates of the current edge.
  228.         if (nverts > 0 ) {
  229.             stopx = vert[2];
  230.             stopy = vert[3] >> 16;
  231.             vert += 2;
  232.         } else  {
  233.             stopx = firstx;     // Last edge, uses first vertex as endpoint
  234.             stopy = firsty;
  235.         }
  236.  
  237.  
  238.         if (stopy < starty )    {
  239.             temp = stopy;
  240.             stopy = starty;
  241.             starty = temp;
  242.             temp = stopx;
  243.             stopx = startx;
  244.             startx = temp;
  245.         }
  246.  
  247.         if (stopy == starty )
  248.         {
  249.             // Draw a edge going horizontally across screen
  250.             if ((stopy >= MINY) && (stopy <=MAXY )) {
  251.                 x1 = startx>>16;
  252.                 x2 = stopx>>16;
  253.  
  254.                 if (x1 > x2 )   {
  255.                     temp = x2;
  256.                     x2 = x1;
  257.                     x1 = temp;
  258.                 }
  259.  
  260.                 if ((x1 <= MAXX ) && (x2 >= MINX))
  261.                 {
  262.                     if (x1 < MINX ) x1 = MINX;
  263.                     if (x2 > MAXX ) x2 = MAXX+1;
  264.                     //gr_uscanline( x1, x2-1, stopy );
  265.                     gr_scanline( x1, x2, stopy );
  266.                 }
  267.             }
  268.         } else  {
  269.  
  270.             dx_dy = (stopx - startx) / (stopy - starty);
  271.  
  272.             if (starty < MINY ) {
  273.                 startx = dx_dy*(MINY-starty)+startx;
  274.                 starty = MINY;
  275.             }
  276.  
  277.             if (stopy > MAXY ) {
  278.                 stopx = dx_dy*(MAXY-starty)+startx;
  279.                 stopy = MAXY+1;
  280.             }
  281.  
  282.             for (; starty < stopy; starty++ )
  283.             {   if (y_edge_list[starty]==-1)
  284.                     y_edge_list[starty] = startx;
  285.                 else    {
  286.                     x1 = y_edge_list[starty]>>16;
  287.                     x2 = startx>>16;
  288.  
  289.                     if (x1 > x2 )   {
  290.                         temp = x2;
  291.                         x2 = x1;
  292.                         x1 = temp;
  293.                     }
  294.  
  295.                     if ((x1 <= MAXX ) && (x2 >= MINX))
  296.                     {
  297.                         if (x1 < MINX ) x1 = MINX;
  298.                         if (x2 > MAXX ) x2 = MAXX+1;
  299.                         //gr_uscanline( x1, x2-1, starty );
  300.                         gr_scanline( x1, x2, starty );
  301.                     }
  302.                 }
  303.                 startx += dx_dy;
  304.             }
  305.         }
  306.  
  307.  
  308.     } while (nverts > 0);
  309. }
  310.  
  311. #endif
  312.